
module.exports = function ($scope,$state, $rootScope,$cookies,httpservice,caseBarDetailBarService,caseBarDetailjinchengService,caseBarDetailZhengService,caseBarDetailxiaService) {

  // /*todo 监听广播，刷新案件进程*/
  $scope.$on('refreshCaseTask', function (event, data) {
    // $scope.getList();

    $scope.detailList = data.detailAll||data.task;
    $scope.bigList[data.parentIndex].taskJsonList.splice(data.index,1,$scope.detailList) ;
  });
  //   /*登录ID*/
    $scope.loginId = kbt.decrypt($cookies.get('cookieUserId'));
    $scope.type = $cookies.get('type');
  //   //获取点击的案件的id
    var caseId = $cookies.get('caseAppId');
  //   //获取点击的案件的type

  //   //当前用户的名字
    $scope.userName=$cookies.get('userPortraitName');
  //   //taskId
    $scope.detailTaskId = '';
  //   //大的列表
    $scope.digList = '';
  //   //详情
    $scope.detailList = '';//用户信息乱码七招的
    $scope.logList = '';//日志
    $scope.plList = '';//评论
    $scope.fileList = ""; //文件
  //
  //   //详情里的阶段
    $scope.detailJd = '';
  //   //律师的列表
    $scope.lawyersList = '';
  //
  //   //详情评论
    $scope.commentInput = '';
  //
  //   //详情界面->任务描述的添加的字
    $scope.desInputText = '';
  //
    var startArr = new Array();
  //


  //拖拽
  $scope.panelConf = {
    animation: 350,
    group: {
      name:'task',
      pull: 'clone',
      put:true
    },
    forceFallback:true,
    handle: '.my-handle',

    onAdd: function (evt) {

      var caseStageId = evt.originalEvent.target.parentElement.attributes['id'].value;
      var list = evt.models;
      var startArr = [];
      for (var i = 0; i < list.length; i++) {
        var taskObj = {
          caseStageId: caseStageId,
          taskId: list[i].task.id,
          sequence: i+1,
        };
        startArr.push(taskObj);
      }
      $scope.returnlistid(startArr);
    },
    onUpdate: function (/**Event*/evt) {
      var list = evt.models;
      var startArr = [];
      for (var i = 0; i < list.length; i++) {
        var taskObj = {
          caseStageId: list[i].task.casestageid,
          taskId: list[i].task.id,
          sequence: i+1,
        };
        startArr.push(taskObj);
      }
      $scope.returnlistid(startArr);
    },
    onRemove: function (/**Event*/evt) {
      var index = evt.originalEvent.target.attributes['index'].value;
      var list = $scope.bigList[index].taskJsonList;
      list.splice(evt.oldIndex, 1);
    },

    // Called when creating a clone of element
    onClone: function (/**Event*/evt) {
      angular.element('.d-closeSiblings').css({"display":"none",'right':'-700px'});
    }
  };


  //   //排序完成请求网络
  $scope.returnlistid = function (startArr) {
    $rootScope.loadingShow = true;
    $rootScope.fadeShow = true;
    caseBarDetailjinchengService.getsortsortable(startArr).then(function (data) {
      if (data.code == 1) {
        // $scope.getList();
      }
      $rootScope.loadingShow = false;
      $rootScope.fadeShow = false;
    }, function (response) {
      $rootScope.loadingShow = false;
      $rootScope.fadeShow = false;
      console.error('updatemessage error', response.status, response.statusText);
    });
  };

    $scope.init = function () {
        /*如果是归档案件不可编辑11*/

        /*用于固定导航条*/
        sessionStorage.navNum = 1;

    };


    $scope.init();

    //解决火狐冲突
    $scope.getFireFocus = function () {
        $('.getFireFocus').focus();
    };

    /*计算页面宽度*/
    function calculationWidth() {
      var len = 400 + parseInt($scope.bigList.length) * 320;
      //如果获取的任务数量太少，则给面板一个固定宽度
      if (len < 1320) {
        $(".CaseDrag").width(1320);
      } else {
        $(".CaseDrag").width(len);
      }
    }
    //获取列表
    $scope.getList = function(){
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailjinchengService.getList(caseId).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                $scope.bigList = data.data;
              calculationWidth();
            }else if(data.code == 0){
              $scope.bigList = null;
            }


        });
    };
    $scope.getList();

  $scope.$watch('bigList', function(newValue, oldValue) {
    $rootScope.bigList = newValue;
  });

    //添加任务
    $scope.addTask = function (allList,inputText) {
        if(inputText != ''&&inputText!=undefined){
            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            var params = {
              "caseId":caseId,
              "caseStageId":allList.id,
              "title":inputText,
            };
          $scope.addTaskInputBo=false;
          caseBarDetailjinchengService.addTask(params).then(function (data) {//inputText,caseId,allList.stageId,null,$scope.loginId,$scope.userName
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if(data.code == 1){

                  $scope.getList();
                  $('.zqNewGreat').css('display','none');
                }
            })
        }else{
          $scope.addTaskInputBo=true;
        }

    };

    $scope.executors = {};
  //
  //   //点击P标签,出现详情界面
  //   $scope.caseDetailShow = function(taskId,jd){
  //
  //       if($('.rightComeTo').css('right')!='0px'){
  //           $('.rightComeTo').animate({
  //               "right": "0"
  //           }, 300);
  //       }
  //
  //
  //       $('.textareaAndSaveBox').addClass('hide');
  //       $('#shuxieqisu').removeClass('hide');
  //       $('#jqisuzhunbei').removeClass('hide');
  //       $('#jqisuzhunbei1').addClass('hide');
  //
  //       $scope.detailTaskId = taskId;
  //       $scope.detailJd = jd;
  //
  //       $rootScope.loadingShow = true;
  //       $rootScope.fadeShow = true;
  //       caseBarDetailjinchengService.getDetailList(taskId).then(function (data) {
  //           $rootScope.loadingShow = false;
  //           $rootScope.fadeShow = false;
  //           if(data.code == 1){
  //               $scope.detailList = data.data.task;
  //               $scope.plList = data.data.caseTaskReviews;
  //               $scope.executors = data.data.taskExecutors;
  //               $scope.logList = data.data.taskLogs;
  //               $scope.fileList = data.data.caseTaskAttses;
  //
  //               $scope.checkList = data.data.caseTaskCheckList;
  //
  //               refreshProgressBar();
  //
  //               //设置详情的复选框是否选中
  //               if($scope.detailList.status == "1"){
  //                   $('#detailcheckbox').attr("checked",true);
  //                   $('#detailcheckbox').checked = true;
  //               }else {
  //                   $('#detailcheckbox').attr("checked",false);
  //               }
  //               //重新设置头像地址
  //               for(var i =0;i<$scope.executors.length;i++){
  //                   if($scope.executors[i].executor.icon != null){
  //                       $scope.executors[i].executor.icon = httpservice.userHeaderImageUrl + $scope.executors[i].executor.icon;
  //                   }
  //               }
  //
  //               //评论头像
  //               for(var i =0;i<$scope.plList.length;i++){
  //                   if($scope.plList[i].commentator.icon != null){
  //                       $scope.plList[i].commentator.icon = httpservice.userHeaderImageUrl + $scope.plList[i].commentator.icon;
  //                   }
  //               }
  //
  //               //给附件地址重新复制
  //               for(var i=0;i<$scope.fileList.length;i++){
  //
  //                   if($scope.fileList[i].filePath != null){
  //                       $scope.fileList[i].filePath = httpservice.userHeaderImageUrl + $scope.fileList[i].filePath;
  //                   }
  //               }
  //
  //               //设置默认的文本框中的文字
  //               $('.jcdetialtextarea').val($scope.detailList.content);
  //
  //           }
  //       })
  //   };
  //
  //   //删除task
    $scope.deteleTask = function () {
        $('.rightComeTo').animate({
            "right": "-640"
        }, 300);
    };

    $scope.jcdeleteupdateNameConfirm = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailjinchengService.updateTask($scope.detailTaskId,"9",$scope.detailList.title,$scope.loginId,$scope.detailList.caseid,$scope.userName).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if(data.code == 1){
                //
                $scope.getList();

                $('#jcdeletedeleteModalpanel').modal('hide');
            }
        })
    }



    //点击复选框.出现横线
    $scope.throthTheP = function ($event,id,title,caseid) {

        var cheackBox = $event.target;
        var $cheackBox = $(cheackBox);

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;

        if(cheackBox.checked){
            $($cheackBox).siblings('.boxRight').children('p').css({
                'text-decoration':'line-through',
                'color':'#00BB9C'
            });
            caseBarDetailjinchengService.updateTask(id,"1",title,$scope.loginId,caseid,$scope.userName).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if(data.code == 1){
                    $scope.getList();
                  $('.rightComeTo').animate({
                    "right": "-700"
                  }, 300);
                }
            });
        }else {
            $($cheackBox).siblings('.boxRight').children('p').css({
                'text-decoration':'none',
                'color':'black'
            });
            caseBarDetailjinchengService.updateTask(id,"0",title,$scope.loginId,caseid,$scope.userName).then(function (data) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if(data.code == 1){
                    $scope.getList();
                  $('.rightComeTo').animate({
                    "right": "-700"
                  }, 300);
                }
            });
        }

        $event.stopPropagation();
    };
  //
  //   //点击详情界面的复选框
  //   $scope.detailThrothTheP = function ($event,id1) {
  //       var cheackBox = $event.target;
  //       var $cheackBox = $(cheackBox);
  //
  //       //$($cheackBox).attr("checked",false);
  //
  //       $rootScope.loadingShow = true;
  //       $rootScope.fadeShow = true;
  //       if(cheackBox.checked){
  //           //$($cheackBox).siblings('.title').css({
  //           //    'text-decoration':'line-through',
  //           //    'color':'#00BB9C'
  //           //});
  //           caseBarDetailjinchengService.updateTask(id1,"1",$scope.detailList.title,$scope.loginId,$scope.detailList.caseid,$scope.userName).then(function (data) {
  //               $rootScope.loadingShow = false;
  //               $rootScope.fadeShow = false;
  //               if(data.code == 1){
  //                   $scope.getList();
  //                   //刷新详情
  //                   $scope.caseDetailShow($scope.detailTaskId,$scope.detailJd);
  //               }
  //           });
  //       }else {
  //           $($cheackBox).siblings('.boxRight').children('p').css({
  //               'text-decoration':'none',
  //               'color':'black'
  //           });
  //           caseBarDetailjinchengService.updateTask(id1,"0",$scope.detailList.title,$scope.loginId,$scope.detailList.caseid,$scope.userName).then(function (data) {
  //               $rootScope.loadingShow = false;
  //               $rootScope.fadeShow = false;
  //               if(data.code == 1){
  //                   $scope.getList();
  //                   //刷新详情
  //                   $scope.caseDetailShow($scope.detailTaskId,$scope.detailJd);
  //               }
  //           });
  //       }
  //   }
    /*页面验证*/
    $scope.validateOptions = {
      blurTrig: true,
      showError: false,
      removeError: true
    };

    //点击新增
    $scope.showAddBox = function ($event) {
        var $a = $($event.target);
        $('.zqNewGreat').css('display','none');
        $('.newAdd').css('display','block');
        var oldHeight = $(window).height()-266 + 'px';
        var newHeight = $(window).height()-332 + 'px';
        $(".g-wrapper").css("max-height",oldHeight);
        $($a).parents('.newAdd').siblings('.newTa').css('display','block');
        $($a).parents('.newAdd').css('display','none');
        $($a).parents('.newAdd').siblings('.g-wrapper').css("max-height",newHeight);

    };

    /*新增阶段*/
    $scope.addStageNew = function () {
        if($rootScope.typeIf!=7){
        $('.addStageAll .addStage').show();
        $('.addStageAll .headerLast').hide();
          $('.fy-input[name="caseStageName"]').focus();

      }

    };
    $scope.addStageNewCancel = function () {
      $('.addStageAll .addStage').hide();
      $('.addStageAll .headerLast').show();

    };

    /*重命名阶段*/
    $scope.editStageShow = function (item) {
      item.showStage = true;
      $scope.caseStageNameCopy = angular.copy(item.name);
    };
    $scope.editStageHide = function (item) {

      item.showStage = false;
      item.name = $scope.caseStageNameCopy;
    };

    //新增阶段
    $scope.createCaseStage = function () {

      var params = {
        "caseId":caseId,
        "caseStageName":$scope.caseStageName
      };
      caseBarDetailjinchengService.createCaseStage(params).then(function (response) {
        if(response.data.code == 1){
          $('.addStageAll .headerLast').show();
          $('.addStageAll .addStage').hide();
          if(!$scope.bigList){
            $scope.bigList = [];
            $scope.bigList.push(response.data.data);
          }else{
            $scope.bigList.push(response.data.data);
          }
          $scope.caseStageName = '';
          calculationWidth();
        }
      });
    };
    //编辑阶段
    $scope.updateCaseStage = function (allList) {
      var params = {
        "caseId":caseId,
        "caseStageName":allList.name,
        "caseStageId":allList.id
      };
      caseBarDetailjinchengService.updateCaseStage(params).then(function (response) {
        if(response.data.code == 1){
          allList.showStage = false;
        }
      });
    };
    //删除阶段
    $scope.deleteCaseStage = function (allList,index) {
      var params = {
        "caseId":caseId,
        "caseStageId":allList.id
      };
      bootbox.dialog({
        message: "您确定删除该阶段吗（包含该阶段下的所有任务）？",
        title: "提示信息",
        buttons: {
          cancel: {
            label: "取消",
            className: "btn-cancel",
            style:'background:red',
            callback: function () {

            }
          },
          confirm: {
            label: "确定",
            className: "btn-confirm",
            callback: function () {
              caseBarDetailjinchengService.deleteCaseStage(params).then(function (response) {
                if(response.data.code == 1){
                  $scope.bigList.splice(index,1);
                }
              });
            }
          }
        }
      });
    };

    //点击取消
    $scope.addCancelBtn = function ($event) {
        var $cancel = $($event.target);
      $('.newAdd').css('display','block');
        $($cancel).parents('.zqNewGreat').css('display','none');
      var oldHeight = $(window).height()-266 + 'px';
      $(".g-wrapper").css("max-height",oldHeight);
    };


    $('.closeDetail').click(function(){
        $(this).parents('.rightComeTo').animate({
            "right": "-650"
        }, 300);
    });

    //P标签点击添加任务
   /* $('.addTaskPClick').click(function () {

        $('#shuxieqisu').addClass('hide');
        $('.textareaAndSaveBox').removeClass('hide');

        $('#jqisuzhunbei1').removeClass('hide');
        $('#jqisuzhunbei').addClass('hide');

    });*/

    //点击任务的名字,出现编辑
    $('.jtitleName').click(function () {
        if ($rootScope.typeIf!=7) {
            $('#shuxieqisu').addClass('hide');
            $('.textareaAndSaveBox').removeClass('hide');

            $('#jqisuzhunbei1').removeClass('hide');
            $('#jqisuzhunbei').addClass('hide');
        }
    });
    //点击取消,显示P标签
    $('.addTaskCancelBtn').click(function () {
        $('.textareaAndSaveBox').addClass('hide');
        $('#shuxieqisu').removeClass('hide');
        $('#jqisuzhunbei').removeClass('hide');
        $('#jqisuzhunbei1').addClass('hide');
    });


    //添加任务人
    /*=====添加律师成员=======*/
    //点击出现BOX
    //$('.showAddPeopleBox').click(function () {
    //    $(".LawyerAddBody").toggle();
    //
    //});
    $(".LawyerList li").on("click",function(){
        var i = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(this).parent().siblings(".LawyerDiv").find(".Lawyerbox").addClass("hide");
        $(this).parent().siblings(".LawyerDiv").find(".Lawyerbox").eq(i).removeClass("hide");
    });
    $(".LawyerAddBody .Add-close").on("click",function(){
        $(".LawyerAddBody").addClass('hide');
    });

    //底部的评论之类的
    $('.pl').click(function () {
        $(this).addClass('clickActive');
        $(this).siblings().removeClass('clickActive');

        $('.plDiv').css('display','block');
        $('.rwrzDiv').css('display','none');

        $('#half2').css('display','block');
    });
    $('.rwrz').click(function () {
        $(this).addClass('clickActive');
        $(this).siblings().removeClass('clickActive');

        $('.rwrzDiv').css('display','block');
        $('.plDiv').css('display','none');

        $('#half2').css('display','block');
    });


    $('.bigBody').click(function () {
        if($('.rightComeTo').css('right')=='0px'){

            $('.rightComeTo').animate({
                "right": "-700"
            }, 300);
        }
    });

    //检查项
  $scope.showCheckItem = {
    CheckItem:false,
    addCheckItem:true,
    delCheckItem:false,
    editCheckItem:false
  };

  //获取div中点击事件
  $('div').mousedown(function(e){//&&$(e.target)!=$('list')
    //使用e.target 判断事件源 是否在 dropDown或者list上 来处理是否隐藏
    if($(e.target)!=$('.boxRight')){
      $('.rightComeTo').show();
    }else if($(e.target)!=$('')){
      $('.rightComeTo').hide();
    }
  });
/*归档案件判断*/
if($rootScope.typeIf==7){
  $('i').click(function(event){
    return false;
  });
}
$scope.checkBoxFalse=function(event){

  return false;
}
  document.body.onselectstart = function(){ return false;}
};
